package com.jimmy.medical.controller;

import com.jimmy.medical.entity.*;
import com.jimmy.medical.model.AjaxResult;
import com.jimmy.medical.model.ContractService;
import com.jimmy.medical.model.SingleService;
import com.jimmy.medical.service.ISaleService;
import com.jimmy.medical.util.AjaxUtils;
import com.jimmy.medical.util.Func_T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    private ISaleService saleService;

    /**
     * 院前服务
     * @return
     */
    @RequestMapping("/singleService")
    @ResponseBody
    public AjaxResult<List<MemberTypeServiceItem>> singleService() {
        return AjaxUtils.process(new Func_T<List<MemberTypeServiceItem>>() {
            @Override
            public List<MemberTypeServiceItem> invoke() throws Exception {
                return saleService.findAllSingleService();
            }
        });
    }

    /**
     * 会籍办理
     * @return
     */
    @RequestMapping("/memberTypeList")
    @ResponseBody
    public AjaxResult<List<MemberType>> memberTypeList() {
        return AjaxUtils.process(new Func_T<List<MemberType>>() {
            @Override
            public List<MemberType> invoke() throws Exception {
                return saleService.findAllMemberType();
            }
        });
    }

    /**
     * 周边产品
     * @return
     */
    @RequestMapping("/commodityList")
    @ResponseBody
    public AjaxResult<List<Commodity>> commodityList() {
        return AjaxUtils.process(new Func_T<List<Commodity>>() {
            @Override
            public List<Commodity> invoke() throws Exception {
                return saleService.findAllCommodity();
            }
        });
    }

    /**
     * 购买单次服务
     * @return
     */
    @RequestMapping("/buySingleService")
    @ResponseBody
    public AjaxResult<RechargeRecordServiceItem> buySingleService(final SingleService singleService) {
        return AjaxUtils.process(new Func_T<RechargeRecordServiceItem>() {
            @Override
            public RechargeRecordServiceItem invoke() throws Exception {
                RechargeRecordServiceItem rest = saleService.getSingleServiceRestCount(singleService);
                if(rest != null && rest.getRestCounts() > 0) {
                    return null;  //还有剩余的服务未使用,请使用完再购买
                }
                RechargeRecordServiceItem item = saleService.saveSingleService(singleService);

                return item;
            }
        });
    }

    /**　
     * 购买会员
     * @return
     */
    @RequestMapping("/buyContractService")
    @ResponseBody
    public AjaxResult<RechargeRecord> buyContractService(final ContractService contractService) {
        return AjaxUtils.process(new Func_T<RechargeRecord>() {
            @Override
            public RechargeRecord invoke() throws Exception {
                RechargeRecord rechargeRecord = saleService.saveContractService(contractService);
                return rechargeRecord;
            }
        });
    }
}
